# -*- coding: utf-8 -*-
import sys
import os
sys.path.append('..')
from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
get_wsgi_application()
from backend.models import Video
from backend.libs import play_info_util
from django.db.models import Q

"""定时查询数据库视频表，更新视频播放地址（阿里点播系统在视频上传后不一定马上能生成播放地址）
    备注：该脚本使用crontab形式启动
    1. 查询app_video表中播放地址不是已 https:// 开始的数据
    2. 调用阿里云点播播放地址函数，获取播放地址
    3. 更新视频的播放地址
"""

def query_no_play_url_videos():
    videos = Video.objects.filter(~Q(url__contains='https://')).all()
    return videos


def main():
    videos = query_no_play_url_videos()
    if not videos:
        return

    for v in videos:
        print(v.url)
        play_info = play_info_util.get_play_info(v.url, v.kind)
        print(play_info)
        if play_info:
            v.url = play_info['PlayURL']
            v.state = 0
            v.save()


if __name__ == '__main__':
    main()